package com.jctrip.hotel.source.service.wehotel.impl;

import java.math.BigDecimal;
import java.util.*;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.stream.Collectors;

import javax.annotation.Resource;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.jctrip.hotel.common.util.OssUtil;
import com.jctrip.hotel.domain.dao.HtBrandStdInfoDao;
import com.jctrip.hotel.domain.entity.*;
import com.jctrip.hotel.source.client.HworldCallBackClient;
import com.jctrip.hotel.source.model.wehotel.req.*;
import com.jctrip.hotel.source.model.wehotel.resq.*;
import com.jctrip.hotel.source.mq.IncrHotelRocketMqProducer;
import com.jctrip.hotel.source.service.basic.HtHotelStdImageService;
import com.jctrip.hotel.source.xxl.wehotel.thread.*;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.jctrip.hotel.common.constant.RedisKeyConstant;
import com.jctrip.hotel.common.constant.XxlJobNameConstant;
import com.jctrip.hotel.common.threadpool.XxlJobThreadPool;
import com.jctrip.hotel.common.util.JsonUtil;
import com.jctrip.hotel.common.util.RedisUtil;
import com.jctrip.hotel.domain.dao.HtHotelStdInfoDao;
import com.jctrip.hotel.common.enums.SourceEnum;
import com.jctrip.hotel.source.model.wehotel.req.HotelInfoData.MapDataResponse;
import com.jctrip.hotel.source.service.basic.HtHotelStdInfoService;
import com.jctrip.hotel.source.service.basic.HtPriceCalendarService;
import com.jctrip.hotel.source.service.basic.HtRoomTypeStdInfoService;
import com.jctrip.hotel.source.service.wehotel.WeHotelService;
import com.jctrip.hotel.source.service.wehotel.WeHotelTaskService;

import lombok.extern.slf4j.Slf4j;

/**
 * @Author henry
 * @Date 2023/6/6 22:36
 * @Description
 */
@Slf4j
@Service
public class WeHotelTaskServiceImpl implements WeHotelTaskService {

    private static final long PUSH_EXPIRY = 1 * 60;
    private static final XxlJobThreadPool hotelIdListTaskPool =
            new XxlJobThreadPool(XxlJobNameConstant.SYNC_WEHOTEL_HOTEL_ID_LIST_TASK_POP);
    private static final XxlJobThreadPool hotelRatesTaskPool =
            new XxlJobThreadPool(XxlJobNameConstant.SYNC_WEHOTEL_HOTEL_RATES_TASK_POP);
    private static final XxlJobThreadPool hotelHotspotRatesTaskPool =
            new XxlJobThreadPool(XxlJobNameConstant.SYNC_WEHOTEL_HOTSPOT_RATES_TASK_POP);
    private static final XxlJobThreadPool hotelRoomRepairTaskPool =
            new XxlJobThreadPool(XxlJobNameConstant.SYNC_WEHOTEL_ROOM_REPAIR_TASK_POP);
    private final static Integer QPS_COUNT = 10;
    private final static Long TIME_OUT = 2000L;
    @Resource
    RedisUtil redisUtil;
    @Autowired
    private WeHotelService weHotelService;
    @Resource
    private HtHotelStdInfoService htHotelStdInfoService;
    @Resource
    private HtHotelStdInfoDao htHotelStdInfoDao;
    @Resource
    private HtPriceCalendarService htPriceCalendarService;
    @Resource
    private HtRoomTypeStdInfoService htRoomTypeStdInfoService;
    @Resource
    private HworldCallBackClient hworldCallBackClient;
    @Resource
    private HtHotelStdImageService htHotelStdImageService;
    @Resource
    private HtBrandStdInfoDao htBrandStdInfoDao;
    @Autowired
    private IncrHotelRocketMqProducer incrHotelRocketMqProducer;

    @Override
    public void syncHotelIdListTaskFastPush(String appId, String key, GetHotelIds getHotelIds) {
        try {
            if (redisUtil.setENx(RedisKeyConstant.HOTEL_MAIN_WEHOTEL_INNID_PUSH, PUSH_EXPIRY,
                    RedisKeyConstant.HOTEL_MAIN_WEHOTEL_INNID_PUSH)) {
                // hotelIdList
                Integer pageNum = getHotelIds.getPageNum();
                while (true) {
                    GetHotelIdsResult getHotelIdsResult = weHotelService.getHotelIdPage(appId, key, getHotelIds);
                    if (null == getHotelIdsResult || CollectionUtils.isEmpty(getHotelIdsResult.getList())) {
                        break;
                    }
                    redisUtil.leftPushAll(RedisKeyConstant.HOTEL_MAIN_WEHOTEL_INNID_LIST,
                            getHotelIdsResult.getList().stream().map(hotelInfoData -> hotelInfoData.getInnId()).collect(
                                    Collectors.toList()));
                    getHotelIds.setPageNum(++pageNum);
                }
            }
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
    }

    @Override
    public void syncHotelInfoListTaskFastPop(String appId, String key) {
        hotelIdListTaskPool.execute(() -> new SyncHotelInfoTaskThread(appId, key));
    }

    @Override
    public void syncHotelInfoTask(String appId, String key, GetHotelInfo getHotelInfo) {
        try {
            HotelInfoData hotelInfoData = weHotelService.getHotelInfo(appId, key, getHotelInfo);
            GetHotelRoomType roomTypeReq = new GetHotelRoomType();
            roomTypeReq.setInnId(hotelInfoData.getInnId());
            HtHotelStdInfo info = parseHotelInfo(hotelInfoData);
            htHotelStdInfoService.saveOrUpdate(info,
                    new LambdaQueryWrapper<HtHotelStdInfo>().eq(HtHotelStdInfo::getHotelId, info.getHotelId())
                            .eq(HtHotelStdInfo::getSourceCode, SourceEnum.WEHOTEL.getCode()));

            // 房型
            List<RoomTypeData> roomTypeData =
                    weHotelService.getHotelRoomTypeResult(appId, key, roomTypeReq).getRoomTypeData();

            roomTypeData.forEach(roomType -> {
                HtRoomTypeStdInfo roomTypeStdInfo = new HtRoomTypeStdInfo();
                roomTypeStdInfo.setHotelId(info.getHotelId());
                roomTypeStdInfo.setRoomTypeCode(roomType.getRoomTypeCode());
                roomTypeStdInfo.setRoomTypeName(roomType.getRoomTypeName());
                roomTypeStdInfo.setMaxCheckIn(roomType.getMaxCheckIn());
                roomTypeStdInfo.setFloor(roomType.getFloor());
                roomTypeStdInfo.setBedWidth(parseHotelBedWidth(roomType.getBedWidth().trim()));
                Integer bedType = null;
                if (roomType.getBedType() != null) {
                    switch (roomType.getBedType()) {
                        case 1:
                            bedType = 2;
                            break;
                        case 2:
                            bedType = 1;
                            break;
                        case 0:
                            bedType = 0;
                            break;
                        case 101:
                        default:
                            bedType = 5;
                            break;
                    }
                }
                roomTypeStdInfo.setBedType(bedType);
                roomTypeStdInfo.setAddBed(roomType.getAddBed());
                roomTypeStdInfo.setWindowType(roomType.getWindow());
                htRoomTypeStdInfoService.saveOrUpdate(roomTypeStdInfo,
                        new LambdaQueryWrapper<HtRoomTypeStdInfo>().eq(HtRoomTypeStdInfo::getHotelId,
                                roomTypeStdInfo.getHotelId())
                                .eq(HtRoomTypeStdInfo::getRoomTypeCode, roomTypeStdInfo.getRoomTypeCode()));
            });

        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
    }

    private HtHotelStdInfo parseHotelInfo(HotelInfoData data) {
        HtHotelStdInfo info = new HtHotelStdInfo();
        info.setSourceCode(SourceEnum.WEHOTEL.getCode());
        info.setHotelId(weHotelService.parseInnId(data.getInnId()));
        info.setHotelName(data.getInnName());
        info.setHotelNameEn(data.getInnNamePinYin());
        info.setBrandCode(data.getBrandCode());
        info.setBrandName(selectBrandName(SourceEnum.WEHOTEL.getCode(), data.getBrandCode()));
        info.setHotelAddress(data.getAddress());
        info.setHotelTel(data.getInnPhone());
        info.setCityName(data.getCityName());
        info.setIntroduction(data.getDescription());
        info.setSourceStatus(data.getStatus() == 1 && data.getBookFlag() == 1 ? 1 : 0);
        info.setSupportForeignGuest(data.getSupportForeignGuest());
        List<MapDataResponse> mapInfo = data.getMapInfo();
        if (!CollectionUtils.isEmpty(mapInfo)) {
            MapDataResponse mapDataResponse = mapInfo.get(0);
            info.setLongitude(BigDecimal.valueOf(mapDataResponse.getLng()));
            info.setLatitude(BigDecimal.valueOf(mapDataResponse.getLag()));
            info.setMapPointType(mapDataResponse.getMapType());
        }
        info.setExtra(JsonUtil.toString(data));
        return info;
    }

    private String selectBrandName(String sourceCode, String brandCode) {
        HtBrandStdInfo htBrandStdInfo = htBrandStdInfoDao.selectOne(new LambdaQueryWrapper<HtBrandStdInfo>()
                .eq(HtBrandStdInfo::getSourceCode, sourceCode)
                .eq(HtBrandStdInfo::getBrandCode, brandCode));

        if (htBrandStdInfo == null) {
            return "";
        }
        return htBrandStdInfo.getBrandName();
    }

    private String parseHotelBedWidth(String bedWidth) {
        if (org.apache.commons.lang3.StringUtils.isBlank(bedWidth)) {
            return "";
        }
        String[] split = bedWidth.split(",");
        if (split.length == 1) {
            return getBedWidth(split[0]);
        }
        StringBuffer buffer = new StringBuffer();
        for (int i = 0; i < split.length; i++) {
            buffer.append(",");
            buffer.append(getBedWidth(split[i]));
        }
        return buffer.substring(1);
    }

    private String getBedWidth(String bedType) {
        String bedWidth;
        switch (bedType) {
            case "0":
                bedWidth = "1";
                break;
            case "1":
                bedWidth = "1.1";
                break;
            case "2":
                bedWidth = "1.2";
                break;
            case "3":
                bedWidth = "1.3";
                break;
            case "4":
                bedWidth = "1.35";
                break;
            case "5":
                bedWidth = "1.4";
                break;
            case "6":
                bedWidth = "1.5";
                break;
            case "7":
                bedWidth = "1.6";
                break;
            case "8":
                bedWidth = "1.65";
                break;
            case "9":
                bedWidth = "1.8";
                break;
            case "10":
                bedWidth = "2";
                break;
            case "11":
                bedWidth = "2.2";
                break;
            case "12":
                bedWidth = "2.3";
                break;
            case "13":
                bedWidth = "2.4";
                break;
            case "14":
                bedWidth = "2.6";
                break;
            default:
                bedWidth = "其他宽度";
                break;
        }
        return bedWidth;
    }

    @Override
    public void syncWehotelRatesTaskPush() {
        try {
            List<HtHotelStdInfo> htHotelStdInfos = htHotelStdInfoDao.selectList(
                    new LambdaQueryWrapper<HtHotelStdInfo>().eq(HtHotelStdInfo::getSourceCode,
                            SourceEnum.WEHOTEL.getCode()).eq(HtHotelStdInfo::getSourceStatus, 1));
            if (!CollectionUtils.isEmpty(htHotelStdInfos)
                    && redisUtil.lGetListSize(RedisKeyConstant.HOTEL_MAIN_WEHOTEL_RATE_INNID_LIST) <= 0) {
                redisUtil.leftPushAll(RedisKeyConstant.HOTEL_MAIN_WEHOTEL_RATE_INNID_LIST,
                        htHotelStdInfos.stream().map(HtHotelStdInfo::getHotelId).collect(Collectors.toList()));
            }
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
    }

    @Override
    public void syncWehotelRatesTaskFastPop(String appId, String key, Integer days) {
        hotelRatesTaskPool.execute(() -> new SyncWehotelRatesTaskThread(appId, key, days));
    }

    @Override
    public void syncWehotelHotspotRatesTaskPush() {
        try {
            if (redisUtil.setENx(RedisKeyConstant.HOTEL_MAIN_WEHOTEL_HOTSPOT_RATE_INNIN_PUSH, PUSH_EXPIRY,
                    RedisKeyConstant.HOTEL_MAIN_WEHOTEL_HOTSPOT_RATE_INNIN_PUSH)) {
                List<String> hotels = hworldCallBackClient.getHotspotHotels().stream()
                        .filter(e -> e.startsWith(SourceEnum.WEHOTEL.getCode())).collect(Collectors.toList());
                if (com.baomidou.mybatisplus.core.toolkit.CollectionUtils.isNotEmpty(hotels)
                        && redisUtil.lGetListSize(RedisKeyConstant.HOTEL_MAIN_WEHOTEL_HOTSPOT_RATE_INNID_LIST) <= 0) {
                    redisUtil.leftPushAll(RedisKeyConstant.HOTEL_MAIN_WEHOTEL_HOTSPOT_RATE_INNID_LIST,
                            new ArrayList<>(hotels));
                }
            }
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
    }

    @Override
    public void syncWehotelHotspotRatesTaskFastPop(String appId, String key) {
        hotelHotspotRatesTaskPool.execute(() -> new SyncWehotelHotspotRatesTaskThread(appId, key));
    }

    @Override
    public void syncWehotelRatesTaskPop(String appId, String key, GetHotelRoomStatus getHotelRoomStatus) {
        GetHotelRoomStatusResult hotelRoomStatusResult =
                weHotelService.getHotelRoomStatusResult(appId, key, getHotelRoomStatus);

        List<RoomTypeInfo> roomTypeList =
                Optional.ofNullable(hotelRoomStatusResult).map(GetHotelRoomStatusResult::getRoomTypeList)
                        .orElse(new ArrayList<>());
        String hotelId = weHotelService.parseInnId(getHotelRoomStatus.getInnId());
        // 找到当前酒店在中台数据库中的记录 先按房型码排序 再按日期排序
        List<HtPriceCalendar> htPriceCalendars = htPriceCalendarService.list(new LambdaQueryWrapper<HtPriceCalendar>()
                .eq(HtPriceCalendar::getHotelId, hotelId)).stream()
                .sorted(Comparator.comparing(HtPriceCalendar::getRoomTypeCode)
                        .thenComparing(HtPriceCalendar::getCalDate))
                .collect(Collectors.toList());
        AtomicBoolean incrFlag = new AtomicBoolean(false);

        roomTypeList.forEach(roomTypeInfo -> {
            Optional.ofNullable(roomTypeInfo.getProductList()).orElse(new ArrayList<>()).forEach(productData -> {
                HtPriceCalendar htPriceCalendar = new HtPriceCalendar();
                htPriceCalendar.setHotelId(hotelId);
                htPriceCalendar.setRoomTypeCode(roomTypeInfo.getRoomTypeCode());
                htPriceCalendar.setRoomTypeName(roomTypeInfo.getRoomTypeName());
                htPriceCalendar.setCalDate(new Date(productData.getEndOfDay()));
                htPriceCalendar.setQuota(productData.getQuota());
                htPriceCalendar.setBreakfastCount(productData.getAdvanceBreakfastCount());
                htPriceCalendar.setRetailPrice(productData.getRackRate());
                htPriceCalendar.setPurchasePrice(productData.getAdvanceRate());
                // 没有变化的时候才走
                if (!incrFlag.get()) {
                    // 获取当前房型 在当前日 中台的数据
                    HtPriceCalendar roomCalDatePriceCalendar =
                            getRoomCalDatePriceCalendar(htPriceCalendar.getRoomTypeCode(), htPriceCalendar.getCalDate(), htPriceCalendars);
                    incrFlag.set(validateIncr(htPriceCalendar.getRetailPrice(), htPriceCalendar.getPurchasePrice(), htPriceCalendar.getQuota(), roomCalDatePriceCalendar));
                }
                htPriceCalendar.setLastCancelTime(new Date(productData.getFreeCancelTime()));
                htPriceCalendar.setCancelRule(JsonUtil.toString(productData.getCancelRule()));
                // 保存房态信息
                htPriceCalendarService.saveOrUpdate(htPriceCalendar,
                        new LambdaQueryWrapper<HtPriceCalendar>().eq(HtPriceCalendar::getHotelId, hotelId)
                                .eq(HtPriceCalendar::getRoomTypeCode, htPriceCalendar.getRoomTypeCode())
                                .eq(HtPriceCalendar::getCalDate, htPriceCalendar.getCalDate()));
            });
        });
        // 如果当前酒店是增量酒店 调用接口通知各渠道
        if (incrFlag.get()) {
            log.info("main-incrHotel={}", hotelId);
            // TODO send mq
            incrHotelRocketMqProducer.send(hotelId);
        }
    }

    @Override
    public void syncWehotelRoomRepairPush() {
        try {
            if (redisUtil.setENx(RedisKeyConstant.HOTEL_MAIN_WEHOTEL_ROOM_REPAIR_PUSH, PUSH_EXPIRY,
                    RedisKeyConstant.HOTEL_MAIN_WEHOTEL_ROOM_REPAIR_PUSH)) {
                List<String> hotels = hworldCallBackClient.getHotspotHotels().stream()
                        .filter(e -> e.startsWith(SourceEnum.WEHOTEL.getCode())).collect(Collectors.toList());
                if (com.baomidou.mybatisplus.core.toolkit.CollectionUtils.isNotEmpty(hotels)
                        && redisUtil.lGetListSize(RedisKeyConstant.HOTEL_MAIN_WEHOTEL_ROOM_REPAIR_LIST) <= 0) {
                    redisUtil.leftPushAll(RedisKeyConstant.HOTEL_MAIN_WEHOTEL_ROOM_REPAIR_LIST,
                            new ArrayList<>(hotels));
                }
            }
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
    }

    @Override
    public void syncWehotelRoomRepairPop(String appId, String key) {
        hotelRoomRepairTaskPool.execute(() -> new SyncWehotelRoomRepairTaskThread(appId, key));
    }

    @Override
    public void syncWehotelRoomRepair(String appId, String key, GetHotelRoomStatus getHotelRoomStatus) {
        String hotelId = weHotelService.parseInnId(getHotelRoomStatus.getInnId());
        // 将当前酒店下所有房型全部置为不可售
        htRoomTypeStdInfoService.update(null,
                new LambdaUpdateWrapper<HtRoomTypeStdInfo>()
                        .set(HtRoomTypeStdInfo::getRoomStatus, 0)
                        .eq(HtRoomTypeStdInfo::getHotelId, hotelId));

        GetHotelRoomStatusResult hotelRoomStatusResult =
                weHotelService.getHotelRoomStatusResult(appId, key, getHotelRoomStatus);

        List<RoomTypeInfo> roomTypeList =
                Optional.ofNullable(hotelRoomStatusResult).map(GetHotelRoomStatusResult::getRoomTypeList)
                        .orElse(new ArrayList<>());

        roomTypeList.forEach(roomTypeInfo -> {
            Optional.ofNullable(roomTypeInfo.getProductList()).orElse(new ArrayList<>()).forEach(productData -> {
                // 将获取到房态的房型状态改为1
                htRoomTypeStdInfoService.update(null,
                        new LambdaUpdateWrapper<HtRoomTypeStdInfo>()
                                .set(HtRoomTypeStdInfo::getRoomStatus, 1)
                                .eq(HtRoomTypeStdInfo::getHotelId, hotelId)
                                .eq(HtRoomTypeStdInfo::getRoomTypeCode, roomTypeInfo.getRoomTypeCode()));
            });
        });
    }

    @Override
    public void syncWehotelPicPush() {
        try {
            if (redisUtil.setENx(RedisKeyConstant.HOTEL_MAIN_WEHOTEL_PIC_PUSH, PUSH_EXPIRY,
                    RedisKeyConstant.HOTEL_MAIN_WEHOTEL_PIC_PUSH)) {
                List<String> hotels = htHotelStdInfoDao.selectList(
                        new LambdaQueryWrapper<HtHotelStdInfo>().eq(HtHotelStdInfo::getSourceCode,
                                SourceEnum.WEHOTEL.getCode())).stream().map(HtHotelStdInfo::getHotelId)
                        .collect(Collectors.toList());
                if (com.baomidou.mybatisplus.core.toolkit.CollectionUtils.isNotEmpty(hotels)
                        && redisUtil.lGetListSize(RedisKeyConstant.HOTEL_MAIN_WEHOTEL_PIC_LIST) <= 0) {
                    redisUtil.leftPushAll(RedisKeyConstant.HOTEL_MAIN_WEHOTEL_PIC_LIST,
                            new ArrayList<>(hotels));
                }
            }
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
    }

    @Override
    public void syncWehotelPicPop(String appId, String key) {
        hotelIdListTaskPool.execute(() -> new SyncWehotelPicTaskThread(appId, key));
    }

    @Override
    public void syncWehotelPic(String appId, String key, String hotelId) {
        while (true) {
            if (StringUtils.isNotBlank(
                    redisUtil.acquireToken(RedisKeyConstant.HOTEL_MAIN_WEHOTEL_PIC_LIMIT_LOCK,
                            QPS_COUNT,
                            TIME_OUT))) {
                GetHotelImage req = new GetHotelImage();
                req.setInnId(weHotelService.parseHotelId(hotelId));
                GetHotelImageResult getHotelImageResult = weHotelService.getHotelImage(appId, key, req);

                if (getHotelImageResult == null) {
                    return;
                }

                int hotelIndex = 0;
                int roomIndex = 0;
                for (ImageData imageData : getHotelImageResult.getImageDatas()) {
                    String url = imageData.getImageUrl();
                    if (imageData.getImageType() == 2) {
                        List<HtHotelStdImage> htHotelStdImage = htHotelStdImageService.list(
                                new LambdaQueryWrapper<HtHotelStdImage>()
                                        .eq(HtHotelStdImage::getHotelId, hotelId)
                                        .eq(HtHotelStdImage::getImageType, 3)
                                        .eq(HtHotelStdImage::getRoomTypeCode, imageData.getRoomTypeCode()));

                        if (!CollectionUtils.isEmpty(htHotelStdImage)) {
                            continue;
                        }
                        uploadPic(url, hotelId, imageData.getRoomTypeCode(), 3, roomIndex++);
                    }

                    // 酒店外观图
                    if (imageData.getImageType() == 3) {
                        List<HtHotelStdImage> htHotelStdImage = htHotelStdImageService.list(
                                new LambdaQueryWrapper<HtHotelStdImage>()
                                        .eq(HtHotelStdImage::getHotelId, hotelId)
                                        .eq(HtHotelStdImage::getImageType, 2)
                                        .eq(HtHotelStdImage::getRoomTypeCode, imageData.getRoomTypeCode())
                                        .eq(HtHotelStdImage::getSourceUrl, url));
                        if (!CollectionUtils.isEmpty(htHotelStdImage)) {
                            continue;
                        }

                        if (imageData.getMaster() == 1) {
                            uploadPic(url, hotelId, null, 1, 0);
                        }
                        uploadPic(url, hotelId, null, 2, hotelIndex++);
                    }
                }
                break;
            }
        }
        try {
            Thread.sleep(1000L);
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage());
        }
    }

    private Boolean validateIncr(BigDecimal retailPrice, BigDecimal purchasePrice, Integer quota, HtPriceCalendar htPriceCalendar) {
        if (Objects.isNull(htPriceCalendar)) {
            return true;
        }

        boolean flag = false;

        if (Objects.nonNull(htPriceCalendar.getQuota())  && Objects.nonNull(quota)) {
            flag = !htPriceCalendar.getQuota().equals(quota) && quota <= 10;
        }

        if (Objects.nonNull(htPriceCalendar.getRetailPrice()) && Objects.nonNull(retailPrice)) {
            flag = htPriceCalendar.getRetailPrice().compareTo(retailPrice) != 0 || flag;
        }

        if (Objects.nonNull(htPriceCalendar.getPurchasePrice()) && Objects.nonNull(purchasePrice)) {
            flag =  htPriceCalendar.getPurchasePrice().compareTo(purchasePrice) != 0 || flag;
        }

        if (Objects.isNull(htPriceCalendar.getQuota()) || Objects.isNull(quota)) {
            flag = true;
        }

        return flag;
    }

    private HtPriceCalendar getRoomCalDatePriceCalendar(String roomType, Date date, List<HtPriceCalendar> htPriceCalendars) {
        return htPriceCalendars.stream()
                .filter(e -> e.getRoomTypeCode().equals(roomType)
                        && DateUtils.isSameDay(e.getCalDate(), date))
                .findFirst().orElse(null);
    }

    private void uploadPic(String sourcePic, String hotelId, String roomType, Integer type, Integer index) {
//        uploadPic(sourcePic, hotelId, roomType, type, index, "png");
//        uploadPic(sourcePic, hotelId, roomType, type, index, "webp");
        uploadPic(sourcePic, hotelId, roomType, type, index, "jpg");
    }

    private void uploadPic(String sourcePic, String hotelId, String roomType, Integer type, Integer index, String suffix) {
        String fileName;
        if (StringUtils.isNotBlank(roomType)) {
            fileName = String.format("WEHOTEL/%s_%s_%s_%s.%s", hotelId, roomType, type, index, suffix);
        } else {
            fileName = String.format("WEHOTEL/%s_%s_%s.%s", hotelId, type, index, suffix);
        }
        String target = OssUtil.updaload(sourcePic, fileName);
        savePicInfo(hotelId, roomType, type, sourcePic, target);
    }


    private void savePicInfo(String hotelId, String roomTypeCode, Integer type, String sourceUrl, String imageUrl) {
        HtHotelStdImage image = new HtHotelStdImage();
        image.setHotelId(hotelId);
        image.setRoomTypeCode(roomTypeCode);
        image.setImageType(type);
        image.setSourceUrl(sourceUrl);
        image.setImageUrl(imageUrl);
        htHotelStdImageService.save(image);
    }
}
